package com.liuweiheng.cloud.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.ObjectUtil;
import com.liuweiheng.base.entity.R;
import com.liuweiheng.cloud.service.NacosService;
import io.netty.util.internal.StringUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

/**
 * 测试调用nacos服务端接口（RestTemplate、OpenFeign）
 * @author lwh
 * @since 2021/1/20
 */
@RestController
@RequestMapping("/nacos/")
public class TestNacosController {

    @Autowired
    private RestTemplate restTemplate;

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }

    @Autowired
    private NacosService nacosService;
    /**
     * Rabbitmq发送消息
     */
    @Autowired
    public RabbitTemplate rabbitTemplate;


    /**
     * 通过RestTemplate调用服务
     * @return
     */
    @GetMapping("consumer")
    public R testNacos() {
        return restTemplate.getForObject("http://nacos-provide/helloNacos", R.class);
    }

    /**
     * 通过OpenFeign调用服务
     * @return
     */
    @GetMapping("feign/consumer")
    public R testOpenFeignNacos() {
        return nacosService.helloNacos();
    }

    @PostMapping("mq/send")
    public R sendMsgToRabbitMq(@RequestParam String msg) {
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", UUID.randomUUID().toString());
        map.put("messageData", msg);
        map.put("createTime", DateUtil.date());
        rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);
        return R.ok();
    }

}
